package com.ph.irp.base.usermanage.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ph.irp.base.usermanage.dao.PBasePurviewFunctionDAO;
import com.ph.irp.base.usermanage.dao.PBasePurviewFunctionroleDAO;
import com.ph.irp.base.usermanage.dao.PBasePurviewFunctiontypeDAO;
import com.ph.irp.base.usermanage.dao.PBasePurviewRoleDAO;
import com.ph.irp.base.usermanage.dao.PBasePurviewUserDAO;
import com.ph.irp.base.usermanage.dao.PBasePurviewUserroleDAO;
import com.ph.irp.base.usermanage.service.UserManageService;
import com.ph.irp.dto.FunctionDTO;
import com.ph.irp.model.PBasePurviewFunction;
import com.ph.irp.model.PBasePurviewFunctiontype;
import com.ph.irp.model.PBasePurviewRole;
import com.ph.irp.model.PBasePurviewUser;

public class UserManageServiceImpl implements UserManageService {
	PBasePurviewUserDAO userDaoProxy;
	PBasePurviewFunctionDAO functionDaoProxy;
	PBasePurviewFunctionroleDAO functionRoleDaoProxy;
	PBasePurviewFunctiontypeDAO functionTypeDaoProxy;
	PBasePurviewRoleDAO roleDaoProxy;
	PBasePurviewUserroleDAO userRoleDaoProxy;

	public void setUserDaoProxy(PBasePurviewUserDAO userDaoProxy) {
		this.userDaoProxy = userDaoProxy;
	}

	public void setFunctionDaoProxy(PBasePurviewFunctionDAO functionDaoProxy) {
		this.functionDaoProxy = functionDaoProxy;
	}

	public void setFunctionRoleDaoProxy(
			PBasePurviewFunctionroleDAO functionRoleDaoProxy) {
		this.functionRoleDaoProxy = functionRoleDaoProxy;
	}

	public void setFunctionTypeDaoProxy(
			PBasePurviewFunctiontypeDAO functionTypeDaoProxy) {
		this.functionTypeDaoProxy = functionTypeDaoProxy;
	}

	public void setRoleDaoProxy(PBasePurviewRoleDAO roleDaoProxy) {
		this.roleDaoProxy = roleDaoProxy;
	}

	public void setUserRoleDaoProxy(PBasePurviewUserroleDAO userRoleDaoProxy) {
		this.userRoleDaoProxy = userRoleDaoProxy;
	}

	@Override
	public List<PBasePurviewRole> getAllUserGroup() throws Throwable {
		return roleDaoProxy.getAllUserGroupList();
	}

	@Override
	public List<PBasePurviewUser> getAllUser() throws Throwable {
		return userDaoProxy.getAllUser();
	}

	public List<PBasePurviewUser> getAllUserExcludeCurrentUserID(int userID)throws Throwable{
		return userDaoProxy.getAllUserExcludeCurrentUserID(userID);
	}
	
	public List<PBasePurviewUser> getUserListByUserName(String userName)
			throws Throwable {
		return userDaoProxy.getUserListByUserName(userName);
	}

	public List<PBasePurviewUser> getUserListByUserNameExcludeCurrentUserID(
			String userName,int userID) throws Throwable {
		return userDaoProxy.getUserListByUserNameExcludeCurrentUserID(userName,userID);
	}

	public List<PBasePurviewRole> getUserGroupListByRoleName(String roleName)
			throws Throwable {
		return roleDaoProxy.getUserGroupListByRoleName(roleName);
	}

	public void addUser(PBasePurviewUser user) throws Throwable {
		userDaoProxy.addUser(user);
	}

	public void addUserGroup(PBasePurviewRole role) throws Throwable {
		roleDaoProxy.addUserRole(role);
	}

	public void addUserRole(String[] userIDs, int roleID) throws Throwable {
		for (String userID : userIDs) {
			userRoleDaoProxy.addUserRole(Integer.parseInt(userID), roleID);
		}
	}

	public PBasePurviewUser getUserByUserId(String userId) throws Throwable {
		return userDaoProxy.getUserByUserId(userId);
	}

	public PBasePurviewRole getRoleByRoleId(String roleId) throws Throwable {
		return roleDaoProxy.getRoleByRoleId(roleId);
	}

	public List<PBasePurviewUser> getUserRoleByRoleId(String roleId,int userID)
			throws Throwable {
		List<PBasePurviewUser> resList = new ArrayList<PBasePurviewUser>();
		List<Integer> userIds = userRoleDaoProxy.getUserRoleByRoleId(roleId);
		for (int userId : userIds) {
			if(userId!=userID){
				PBasePurviewUser user = userDaoProxy.getUserByUserId(String
						.valueOf(userId));
				resList.add(user);
			}	
		}
		return resList;
	}

	public List<PBasePurviewUser> getUserRoleNotByRoleId(String roleId,int userID)
			throws Throwable {
		List<PBasePurviewUser> resList = new ArrayList<PBasePurviewUser>();
		List<Integer> allUserIds = userDaoProxy.getAllUserIds();
		List<Integer> userIds = userRoleDaoProxy.getUserRoleByRoleId(roleId);
		allUserIds.removeAll(userIds);
		for (int userId : allUserIds) {
			if(userId!=userID){
			PBasePurviewUser user = userDaoProxy.getUserByUserId(String
					.valueOf(userId));
			resList.add(user);
			}
		}
		return resList;
	}

	public void editUser(PBasePurviewUser user) throws Throwable {
		userDaoProxy.editUser(user);
	}

	public void editUserRole(PBasePurviewRole role) throws Throwable {
		roleDaoProxy.editUserRole(role);
	}

	public void deleteUser(String deleteIds) throws Throwable {
		userRoleDaoProxy.deleteUserRoleByUserIDs(deleteIds);
		userDaoProxy.deleteUser(deleteIds);
	}

	public void deleteUserGroup(String deleteIds) throws Throwable {
		userRoleDaoProxy.deleteUserRoleByRoleIDs(deleteIds);
		for (String roleID : deleteIds.split(",")) {
			functionRoleDaoProxy.deleteFunctionRoleByRoleID(roleID);
		}
		roleDaoProxy.deleteUserGroup(deleteIds);
	}

	public int getRoleIdByRoleNameAndDesc(String roleName, String roleDesc)
			throws Throwable {
		return roleDaoProxy.getRoleIdByRoleNameAndDesc(roleName, roleDesc);
	}

	public void deleteUserRole(int roleId) throws Throwable {
		userRoleDaoProxy.deleteUserRole(roleId);
	}

	public List<Integer> getFunctionByRoleID(String roleID) throws Throwable {
		return functionRoleDaoProxy.getFunctionByRoleID(roleID);
	}

	@SuppressWarnings("unchecked")
	public Map getFunctionType() throws Throwable {
		List<PBasePurviewFunctiontype> functionTypeList = functionTypeDaoProxy
				.getFunctionTypeByFunctionParentTypeID(0);
		Map functionTypeMap = new HashMap();
		for (PBasePurviewFunctiontype ft : functionTypeList) {
			List<PBasePurviewFunctiontype> subFunctionTypeList = functionTypeDaoProxy
					.getFunctionTypeByFunctionParentTypeID(ft
							.getFunctionTypeId());
			functionTypeMap.put(ft, subFunctionTypeList);
		}
		return functionTypeMap;
	}

	public List<FunctionDTO> getFunctionTypeByGroupID(String roleID)
			throws Throwable {
		List<Integer> functionTypeIDs = functionRoleDaoProxy
				.getFunctionByRoleID(roleID);

		return getFunctionDTOListByFunctionTypeIDs(functionTypeIDs);
	}

	public List<FunctionDTO> getFunctionTypeByFunctionTypeID(
			String functionTypeID) throws Throwable {
		List<PBasePurviewFunctiontype> functionTypeList = functionTypeDaoProxy
				.getFunctionTypeByFunctionParentTypeID(Integer
						.parseInt(functionTypeID));

		return getFunctionDTOListByFunctionTypeList(functionTypeList);
	}
	
	public String getFunctionTypeDescByFunctionTypeName(String functionTypeName) throws Throwable{
		return functionTypeDaoProxy.getFunctionTypeDescByFunctionTypeName(functionTypeName);
	}

	public List<FunctionDTO> getAllFunctionTypeByFunctionTypeID()
			throws Throwable {
		List<PBasePurviewFunctiontype> functionTypeList = functionTypeDaoProxy
				.getAllFunctionType();
		return getFunctionDTOListByFunctionTypeList(functionTypeList);
	}

	private List<FunctionDTO> getFunctionDTOListByFunctionTypeList(
			List<PBasePurviewFunctiontype> functionTypeList) throws Throwable {
		List<FunctionDTO> functionDTOList = new ArrayList<FunctionDTO>();
		List<PBasePurviewFunction> functionList = null;
		List<Integer> roleIDs = null;
		String functionTypeName = null;
		String userGroupName = null;
		for (PBasePurviewFunctiontype functionType : functionTypeList) {
			functionTypeName = functionType.getFunctionType();
			functionList = functionDaoProxy
					.getFunctionByFunctionTypeID(functionType
							.getFunctionTypeId());
			roleIDs = functionRoleDaoProxy
					.getRoleIDsByFunctionTypeID(functionType
							.getFunctionTypeId());

			for (int roleID : roleIDs) {
				userGroupName = userRoleDaoProxy
						.getUserRoleNameByRoleId(roleID);
				for (PBasePurviewFunction function : functionList) {
					FunctionDTO fDTO = new FunctionDTO();

					fDTO.setUserGroupName(userGroupName);
					fDTO.setFunctionDesc(function.getFunctionDesc());
					fDTO.setFunctionName(function.getFunctionName());
					fDTO.setFunctionTypeName(functionTypeName);

					functionDTOList.add(fDTO);
				}
			}
		}

		return functionDTOList;
	}

	public String getFunctionTypeNameByFunctionTypeID(String functionTypeID)
			throws Throwable {
		return functionTypeDaoProxy.getFunctionTypeByFunctionTypeID(Integer
				.parseInt(functionTypeID));
	}

	public List<FunctionDTO> getAllFunctionType() throws Throwable {
		List<Integer> functionTypeIDs = functionRoleDaoProxy
				.getAllFunctionTypeID();
		return getFunctionDTOListByFunctionTypeIDs(functionTypeIDs);
	}

	private List<FunctionDTO> getFunctionDTOListByFunctionTypeIDs(
			List<Integer> functionTypeIDs) throws Throwable {
		List<FunctionDTO> fList = new ArrayList<FunctionDTO>();
		for (int functionTypeID : functionTypeIDs) {
			List<PBasePurviewFunction> functionList = functionDaoProxy
					.getFunctionByFunctionTypeID(functionTypeID);
			String functionTypeName = functionTypeDaoProxy
					.getFunctionTypeByFunctionTypeID(functionTypeID);
			String parentFunctionTypeName = functionTypeDaoProxy
					.getFunctionTypeByFunctionTypeID(functionTypeDaoProxy
							.getFunctionParentTypeIDByFunctionTypeID(functionTypeID));
			for (PBasePurviewFunction function : functionList) {
				FunctionDTO fDTO = new FunctionDTO();
				fDTO.setFunctionDesc(function.getFunctionDesc());
				fDTO.setFunctionName(function.getFunctionName());
				fDTO.setFunctionTypeName(functionTypeName);
				fDTO.setParentFunctionTypeName(parentFunctionTypeName);
				fList.add(fDTO);
			}

		}
		return fList;
	}

	public void editUserPurview(String editFunctionTypeID,
			String editUserGroupID) throws Throwable {
		String[] editFunctionTypeIDs;
		if (editUserGroupID != null && !editUserGroupID.equals("")) {
			functionRoleDaoProxy.deleteFunctionRoleByRoleID(editUserGroupID);
			if (editFunctionTypeID != null && !editFunctionTypeID.equals("")) {
				editFunctionTypeIDs = editFunctionTypeID.split(",");

				for (String functionTypeID : editFunctionTypeIDs) {
					functionRoleDaoProxy.addFunctionRole(functionTypeID,
							editUserGroupID);
				}
			}
		}
	}
	
	public List<Integer> getUserRoleByUserId(String userId) throws Throwable{
		return userRoleDaoProxy.getUserRoleByUserId(userId);
	}
}
